package org.aphelie.chronicle.ui.forms;

import org.aphelie.chronicle.ArcTopic;
import org.aphelie.chronicle.ui.ChronicleUIPlugin;
import org.aphelie.chronicle.ui.editors.ArcFormEditor;
import org.aphelie.chronicle.ui.layout.ArcFormLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

public class ArcTopicSection extends ArcFormSection {
	
	protected ArcTopic topic;
	
	private ArcTopicPage page;
	
	public ArcTopicSection(ArcTopicPage page, Composite parent, ArcTopic topic) {
		this(page, parent, topic, SWT.NULL);
	}
	
	public ArcTopicSection(ArcTopicPage page, Composite parent, ArcTopic topic, int style) {
		super(page, parent, style);
		this.page = page;
		this.topic = topic;
		initialize(page.getManagedForm()); // TODO Move upper
		createClient(getSection());
	}
	
	ArcTopic getTopic() {
		return topic;
	}
	
	@Override // TODO Check
	protected FormToolkit getToolkit() {
		return page.getToolkit();
	}
	
	ArcFormEditor getEditor() {
		return (ArcFormEditor) page.getEditor();
	}

	protected void createClient(Section section) {
		section.setText(topic.getLabel());
		section.setLayout(ArcFormLayoutFactory.createClearTableWrapLayout(true, 4));
		TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB);
        section.setLayoutData(data);
        section.setDescription("ArcTupleSection.Description");
        ChronicleUIPlugin.getDefault().setCompositeBackgroundColor(section);
        fillBody(section);
       
        /*Composite client = getToolkit().createComposite(section);
        client.setLayout(ArcFormLayoutFactory.createSectionClientTableWrapLayout(false, 2));
        section.setClient(client);
        fillBody(client);*/
	}
	
	protected void fillBody(Section section) {
		Composite body = getToolkit().createComposite(section);
		int nCol = topic.eContents().size();
		body.setLayout(ArcFormLayoutFactory.createSectionClientTableWrapLayout(true, nCol));
		section.setClient(body);
		 ArcSectionFactory.fillBody((ArcTopicPage)getPage(), body, topic);
        
		/*for(EObject content : topic.eContents()) {
			System.err.println(content);
			if (content instanceof ArcScore) {
				ScoreEntry entry = new ScoreEntry(body, getToolkit(), this, (ArcScore) content);
			} else if (content instanceof ArcTopic) {
				ArcTopic t = (ArcTopic) content;
				if (t.getGrade() == 1) {
					ArcTopicSection scores = new ArcScoresSection((ArcTopicPage) getPage(), body, t);
					page.managedForm.addPart(scores);
				} else {
					ArcTopicSection child = new ArcTopicSection((ArcTopicPage) getPage(), body, (ArcTopic) content);
					page.managedForm.addPart(child);	
				}
			}
			       
		}*/
	}
	
}
